document.getElementById("search").onkeypress = function(e){
	if(e.key == 'Enter'){
		window.location.href = "/file/search/key/"+this.value;
	}
};

var getImageFromUrl = function(url, x, callback) {
	var img = new Image, data, ret={data: null, pending: true};
	
	img.onError = function() {
		throw new Error('Cannot load image: "'+url+'"');
	}
	img.onload = function() {
		var canvas = document.createElement('canvas');
		document.body.appendChild(canvas);
		canvas.width = img.width;
		canvas.height = img.height;

		var ctx = canvas.getContext('2d');
		ctx.drawImage(img, 0, 0);
		// Grab the image as a jpeg encoded in base64, but only the data
		data = canvas.toDataURL('image/jpeg').slice('data:image/jpeg;base64,'.length);
		// Convert the data to binary form
		data = atob(data)
		document.body.removeChild(canvas);

		ret['data'] = data;
		ret['pending'] = false;
		if (typeof callback === 'function') {
			callback(data,img.width, img.height, x);
		}
	}
	img.src = url;

	return ret;
}
var doc;
var createPDF = function(imgData, width, height, x) {
	doc = new jsPDF();
	doc.addImage(imgData, 'JPEG',2,10, 206, height*206/width);

	// Output as Data URI
	var str = doc.output('datauri');
	var iframe = "<iframe width='100%' height='100%' src='" + str + "'></iframe>"
	x.document.open();
	x.document.write(iframe);
	x.document.close();
}

function downLoad(id) {
	var x = window.open();
	$.ajax({
		type: "get",
		url: "/file/loadimage/id/"+id,
		success:function(data){
			var str = JSON.parse(data);
			var url = JSON.parse(str.compressinfo);
			console.log(url);
			getImageFromUrl(url, x, createPDF);
		}
	});
}


var Status = true;
function showList() {
	Status = !Status;
	if(Status){
		document.getElementById("list").style.display = 'block';
		document.getElementById("listByType").style.display = 'none';
	}
	else{
		document.getElementById("list").style.display = 'none';
		document.getElementById("listByType").style.display = 'block';
	}
}

